package common.base.dao;

import java.lang.reflect.ParameterizedType;
import java.util.List;

import javax.annotation.Resource;

import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Projections;
import org.springframework.transaction.annotation.Transactional;

import common.base.BaseVo;
import common.tools.StringTool;

/*
 * 公共方法实现类
 */
@SuppressWarnings("unchecked")
@Transactional
public abstract class IBaseDaoImpl<T,S extends BaseVo> implements IBaseDao<T,S>{
   
    //注入sessionfactory
    @Resource
    public  SessionFactory sessionFactory;
    Class clazz;
    

    //构造方法：获取T的真实类型
    public IBaseDaoImpl(){
        ParameterizedType pType=(ParameterizedType) this.getClass().getGenericSuperclass();
        clazz=(Class) pType.getActualTypeArguments()[0];
        System.out.print(clazz.getSimpleName());
    }
    
    
    public Criteria PagingSort(Criteria c,S s){
    	 //排序  
        String sortIdx = s.getSidx();  
        if (StringTool.isStringAvaliable(sortIdx)) {  
            if ("asc".equalsIgnoreCase(s.getSord())) {  
                c.addOrder(Order.asc(sortIdx));  
            } else {  
                c.addOrder(Order.desc(sortIdx));  
            }  
        }  
          
        c.setFirstResult((s.getPage() - 1) * s.getRows());  
        c.setMaxResults(s.getRows());
        return c;
    }
    
    @Override
    public   List<T> queryForPageInfo(S searchVo){
    	    Session session = sessionFactory.getCurrentSession();  
	        Criteria c = getSearchCriteria(session, searchVo);  
	        c=PagingSort(c, searchVo);//分页与排序
	        return (List<T>)c.list();
    }
    
    @Override
    public int count(S searchVo) {  
        Session session = sessionFactory.getCurrentSession();  
        Criteria c = getSearchCriteria(session, searchVo);  
          
        c.setProjection(Projections.rowCount());  
  
        return ((Long)c.list().get(0)).intValue();  
    } 
    /**
     * 拼接搜索条件
     * @param session
     * @param searchVo
     * @return
     */
    @Override
    public abstract Criteria getSearchCriteria(Session session, S searchVo);
	/*
     * 删除对象
     * (non-Javadoc)
     * @see com.ssh.SpringMVC.Dao.IBaseDao#delete(int)
     */
    @Override
    public void delete(int id) {
        // TODO Auto-generated method stub
        sessionFactory.getCurrentSession().delete(
                sessionFactory.getCurrentSession().get(clazz, id));   
        sessionFactory.getCurrentSession().flush();
    }

    /*
     * 查询所有对象
     * (non-Javadoc)
     * @see com.ssh.SpringMVC.Dao.IBaseDao#getObjectALL()
     */
    @Override
    public List<T> getObjectALL() {
//        System.out.println("=====："+"from"+clazz.getSimpleName());
//        System.out.println("=====："+"from    "+clazz.getSimpleName());
//        System.out.println("--------------"+clazz.getSimpleName());
        return sessionFactory.getCurrentSession().createQuery("from "+clazz.getSimpleName()).list();
    }
      /*
       * 根据id获取对象
       * (non-Javadoc)
       * @see com.ssh.SpringMVC.Dao.IBaseDao#getObjectByid(int)
       */
    @Override
    public T getObjectByid(int id) {
        // TODO Auto-generated method stub
        return (T) sessionFactory.getCurrentSession().get(clazz, id);
    }

    /*
     * 保存对象
     * (non-Javadoc)
     * @see com.ssh.SpringMVC.Dao.IBaseDao#save(java.lang.Object)
     */
    @Override
    public void save(T t) {
        // TODO Auto-generated method stub
    sessionFactory.getCurrentSession().save(t);    
    }

    @Override
    public void update(T t) {
        // TODO Auto-generated method stub
        sessionFactory.getCurrentSession().update(t);
        sessionFactory.getCurrentSession().flush();
    }

}